// Lerner, Michael. "Local power: Understanding the adoption and design of county wind energy regulation." Review of Policy Research. Forthcoming.
//
// Analysis file

clear
cd "Y:/Google Drive/My Drive/Research/wind_reg/ROPR Replication Materials"

// Install required dependencies
ssc install estout
ssc install mimrgns

// Load analysis data
use "analysis_data_3Aug2021", clear

// Drop Hawaii counties because missing wind potential data
drop if years_viable ==.

// Encode state_fe as factor variable
encode state_fe, gen(i_state_fe)

// Generate years_viable_binary for 6-10 years (1, 0 otherwise)
gen i_years_viable_binary_6_10_years = 0
quietly replace i_years_viable_binary_6_10_years = 1 if years_viable==3

// Generate years_viable_binary for >10 years (1, 0 otherwise)
gen i_years_viable_binary = 0
quietly replace i_years_viable_binary = 1 if years_viable==4

* Impute data

mi set mlong

misstable summarize, gen(miss_)

mi register imputed setback  extraction_gdp local_government_jobs

mi impute chained (ologit, augment) setback (pmm, knn(10)) extraction_gdp local_government_jobs = nb_setback_existence i.nb_setback_stringency_median i.years_viable population_density county_gdp republican_vote transmission_distance nb_wind_farm_existence nb_wind_farm_count i.i_state_fe, add(50) rseed(12312) dots

* Main model

mi estimate, cmdok errorok post: capture zioprobit setback i.years_viable i.nb_setback_stringency_median nb_wind_farm_count county_gdp local_government_jobs extraction_gdp population_density republican_vote transmission_distance i.i_state_fe, inflate(i.years_viable i.nb_setback_existence nb_wind_farm_existence county_gdp local_government_jobs extraction_gdp population_density republican_vote transmission_distance i.i_state_fe) robust iterate(300)

estimates store main

* Marginal effects for non-participation
mimrgns, predict(ppar) dydx(i.years_viable i.nb_setback_existence nb_wind_farm_existence) post
est store margin_inflate

* Marginal effects for stringency conditional on participation
forvalues i = 1/5 {
	mi estimate, cmdok errorok post: capture zioprobit setback i.years_viable i.nb_setback_stringency_median nb_wind_farm_count county_gdp local_government_jobs extraction_gdp population_density republican_vote transmission_distance i.i_state_fe, inflate(i.years_viable i.nb_setback_existence nb_wind_farm_existence county_gdp local_government_jobs extraction_gdp population_density republican_vote transmission_distance i.i_state_fe) robust iterate(300)

	mimrgns, predict(pcond1 outcome(`i')) dydx(i.years_viable i.nb_setback_stringency_median nb_wind_farm_count) post
	est store margin_oprobit_`i'
}

* Robustness check: Setbacks in log feet (bans as 5280ft setbacks)
* Cragg hurdle linear model with robust standard errors
preserve

use "analysis_data_3Aug2021", clear

// Drop Hawaii counties because missing wind potential data
drop if years_viable ==.

// Encode state_fe as factor variable
encode state_fe, gen(i_state_fe)

// Perform inverse-sine transformation (similar to log) for numeric setbacks
replace setback_numeric = asinh(setback_numeric)

* Impute
mi set mlong

misstable summarize, gen(miss_)

mi register imputed setback_numeric extraction_gdp local_government_jobs

mi impute chained (pmm, knn(10)) setback_numeric (pmm, knn(10)) extraction_gdp local_government_jobs = nb_setback_existence i.nb_setback_stringency_median i.years_viable population_density county_gdp republican_vote transmission_distance nb_wind_farm_existence nb_wind_farm_count i.i_state_fe, add(50) rseed(12312) dots

mi estimate, cmdok errorok post: capture churdle linear setback_numeric i.years_viable i.nb_setback_stringency_median nb_wind_farm_count county_gdp local_government_jobs extraction_gdp population_density republican_vote transmission_distance i.i_state_fe, select(i.years_viable i.nb_setback_existence nb_wind_farm_existence county_gdp local_government_jobs extraction_gdp population_density republican_vote transmission_distance i.i_state_fe) ll(0) vce(robust)
estimates store r_numericsetbacks

restore

* Robustness check: Binary years_viable

mi estimate, cmdok errorok post: capture zioprobit setback i_years_viable_binary i.nb_setback_stringency_median nb_wind_farm_count county_gdp local_government_jobs extraction_gdp population_density republican_vote transmission_distance i.i_state_fe, inflate(i_years_viable_binary i.nb_setback_existence nb_wind_farm_existence county_gdp local_government_jobs extraction_gdp population_density republican_vote transmission_distance i.i_state_fe) robust iterate(300)
estimates store r_binary


* Robustness check: Low acceptance rate (need 100% more land for viability)

mi estimate, cmdok errorok post: capture zioprobit setback i.years_viable_lowaccept i.nb_setback_stringency_median nb_wind_farm_count county_gdp local_government_jobs extraction_gdp population_density republican_vote transmission_distance i.i_state_fe, inflate(i.years_viable_lowaccept i.nb_setback_existence nb_wind_farm_existence county_gdp local_government_jobs extraction_gdp population_density republican_vote transmission_distance i.i_state_fe) robust iterate(300)
estimates store r_lowaccept

* Robustness check: High acceptance rate (need 50% less land for viability)

mi estimate, cmdok errorok post: capture zioprobit setback i.years_viable_highaccept i.nb_setback_stringency_median nb_wind_farm_count county_gdp local_government_jobs extraction_gdp population_density republican_vote transmission_distance i.i_state_fe, inflate(i.years_viable_highaccept i.nb_setback_existence nb_wind_farm_existence county_gdp local_government_jobs extraction_gdp population_density republican_vote transmission_distance i.i_state_fe) robust iterate(300)
estimates store r_highaccept

* Robustness check: Percentage of neighbors with setbacks

mi estimate, cmdok errorok post: capture zioprobit setback i.years_viable i.nb_setback_stringency_median nb_wind_farm_count county_gdp local_government_jobs extraction_gdp population_density republican_vote transmission_distance i.i_state_fe, inflate(i.years_viable nb_setback_existence_p nb_wind_farm_existence county_gdp local_government_jobs extraction_gdp population_density republican_vote transmission_distance i.i_state_fe) robust iterate(300)
estimates store r_nbexistsp

* Robustness check: Maximum of neighbor stringency

mi estimate, cmdok errorok post: capture zioprobit setback i.years_viable i.nb_setback_stringency_max nb_wind_farm_count county_gdp local_government_jobs extraction_gdp population_density republican_vote transmission_distance i.i_state_fe, inflate(i.years_viable i.nb_setback_existence nb_wind_farm_existence county_gdp local_government_jobs extraction_gdp population_density republican_vote transmission_distance i.i_state_fe) robust iterate(300)
estimates store r_nbstringmax

* Robustness check: Neighbor wind farm capacity

mi estimate, cmdok errorok post: capture zioprobit setback i.years_viable i.nb_setback_stringency_median nb_wind_farm_capacity county_gdp local_government_jobs extraction_gdp population_density republican_vote transmission_distance i.i_state_fe, inflate(i.years_viable i.nb_setback_existence nb_wind_farm_existence county_gdp local_government_jobs extraction_gdp population_density republican_vote transmission_distance i.i_state_fe) robust iterate(300)
estimates store r_nbwindcap

* Robustness check: Hawaii counties as 0 years_viable

preserve

use "analysis_data_3Aug2021", clear

// Treat Hawaii counties as 0 years_viable
replace years_viable = 1 if state == "hawaii"

// Encode state_fe as factor variable
encode state_fe, gen(i_state_fe)

* Impute data

mi set mlong

misstable summarize, gen(miss_)

mi register imputed setback  extraction_gdp local_government_jobs

mi impute chained (ologit, augment) setback (pmm, knn(10)) extraction_gdp local_government_jobs = nb_setback_existence i.nb_setback_stringency_median i.years_viable population_density county_gdp republican_vote transmission_distance nb_wind_farm_existence nb_wind_farm_count i.i_state_fe, add(50) rseed(12312) dots

mi estimate, cmdok errorok post: capture zioprobit setback i.years_viable i.nb_setback_stringency_median nb_wind_farm_count county_gdp local_government_jobs extraction_gdp population_density republican_vote transmission_distance i.i_state_fe, inflate(i.years_viable i.nb_setback_existence nb_wind_farm_existence county_gdp local_government_jobs extraction_gdp population_density republican_vote transmission_distance i.i_state_fe) robust iterate(300)
estimates store r_hawaiiasnotviable

restore

* Robustness check: Hawaii counties as >10 years_viable

preserve
use "analysis_data_3Aug2021", clear

// Treat Hawaii counties as >10 years_viable
replace years_viable = 4 if state == "hawaii"

// Encode state_fe as factor variable
encode state_fe, gen(i_state_fe)

* Impute data

mi set mlong

misstable summarize, gen(miss_)

mi register imputed setback  extraction_gdp local_government_jobs

mi impute chained (ologit, augment) setback (pmm, knn(10)) extraction_gdp local_government_jobs = nb_setback_existence i.nb_setback_stringency_median i.years_viable population_density county_gdp republican_vote transmission_distance nb_wind_farm_existence nb_wind_farm_count i.i_state_fe, add(5) rseed(12312) dots

mi estimate, cmdok errorok post: capture zioprobit setback i.years_viable i.nb_setback_stringency_median nb_wind_farm_count county_gdp local_government_jobs extraction_gdp population_density republican_vote transmission_distance i.i_state_fe, inflate(i.years_viable i.nb_setback_existence nb_wind_farm_existence county_gdp local_government_jobs extraction_gdp population_density republican_vote transmission_distance i.i_state_fe) robust iterate(300)

estimates store r_hawaiiasmaxviable

restore

* Robustness check: Counties without websites as no setback

preserve
use "analysis_data_3Aug2021", clear

// Drop Hawaii counties because missing wind potential data
drop if years_viable ==.

// Encode state_fe as factor variable
encode state_fe, gen(i_state_fe)

// Treat counties without websites as having no setback
replace setback = 1 if setback_detail == "no_website"

* Impute data

mi set mlong

misstable summarize, gen(miss_)

mi register imputed setback  extraction_gdp local_government_jobs

mi impute chained (ologit, augment) setback (pmm, knn(10)) extraction_gdp local_government_jobs = nb_setback_existence i.nb_setback_stringency_median i.years_viable population_density county_gdp republican_vote transmission_distance nb_wind_farm_existence nb_wind_farm_count i.i_state_fe, add(50) rseed(12312) dots

mi estimate, cmdok errorok post: capture zioprobit setback i.years_viable i.nb_setback_stringency_median nb_wind_farm_count county_gdp local_government_jobs extraction_gdp population_density republican_vote transmission_distance i.i_state_fe, inflate(i.years_viable i.nb_setback_existence nb_wind_farm_existence county_gdp local_government_jobs extraction_gdp population_density republican_vote transmission_distance i.i_state_fe) robust iterate(300)

estimates store r_nowebsiteasnone

restore

* Robustness check: Counties without websites as bans

preserve
use "analysis_data_3Aug2021", clear


// Drop Hawaii counties because missing wind potential data
drop if years_viable ==.

// Encode state_fe as factor variable
encode state_fe, gen(i_state_fe)

// Treat counties without websites as bans
replace setback = 5 if setback_detail == "no_website"

* Impute data

mi set mlong

misstable summarize, gen(miss_)

mi register imputed setback  extraction_gdp local_government_jobs

mi impute chained (ologit, augment) setback (pmm, knn(10)) extraction_gdp local_government_jobs = nb_setback_existence i.nb_setback_stringency_median population_density county_gdp republican_vote transmission_distance nb_wind_farm_existence nb_wind_farm_count i.i_state_fe, add(50) rseed(12312) dots

mi estimate, cmdok errorok post: capture zioprobit setback i.years_viable i.nb_setback_stringency_median nb_wind_farm_count county_gdp local_government_jobs extraction_gdp population_density republican_vote transmission_distance i.i_state_fe, inflate(i.years_viable i.nb_setback_existence nb_wind_farm_existence county_gdp local_government_jobs extraction_gdp population_density republican_vote transmission_distance i.i_state_fe) robust iterate(300)

estimates store r_nowebsiteasbans

restore

* Make tables
* Main results

estout main using "Results/main_3Aug2021.csv", replace varwidth(25) collabels("") cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) stats(N, fmt(%9.3f %9.0f) labels("N")) starlevels(* 0.10 ** 0.05 *** 0.01) style(tex) 

estout margin_* using "Results/main_margins_3Aug2021.csv", replace varwidth(25) collabels("") cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) stats(N, fmt(%9.3f %9.0f) labels("N")) starlevels(* 0.10 ** 0.05 *** 0.01) style(tex) 

* Robustness checks
estout r_* using "Results/robustness_3Aug2021.csv", replace varwidth(25) collabels("") cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) stats(N aic, fmt(%9.3f %9.0f) labels("N" "AIC")) starlevels(* 0.10 ** 0.05 *** 0.01) style(tex)

//
//
// Sensitivity analysis for counties without websites
// Note: This can take a while.

// Load analysis data
use "analysis_data_3Aug2021", clear

// Drop Hawaii counties because missing wind potential data
drop if years_viable ==.

// Encode state_fe as factor variable
encode state_fe, gen(i_state_fe)

// Create a program where 1) a random proportion of the counties missing websites are treated as having either no setback or a ban, 2) the main regression is run, and 3) the coefficient estimates are saved

capture program drop myboot

program define myboot, rclass
	
	args pval
	
	capture restore
	
	local first=ceil(uniform()*10000)

	set seed `first'
	
	preserve
	
	quietly gen rannum = uniform()
	
	quietly replace setback = 5 if setback_detail == "no_website" & rannum <= `pval'
	
	quietly mi set mlong

	capture mi register imputed setback extraction_gdp local_government_jobs

	quietly mi impute chained (ologit, augment) setback (pmm, knn(10)) extraction_gdp local_government_jobs = nb_setback_existence i.nb_setback_stringency_median i.years_viable population_density county_gdp republican_vote transmission_distance nb_wind_farm_existence nb_wind_farm_count i.i_state_fe, add(25) rseed(12312) dots
   
	mi estimate, cmdok errorok: capture zioprobit setback i.years_viable i.nb_setback_stringency_median nb_wind_farm_count county_gdp local_government_jobs extraction_gdp population_density republican_vote transmission_distance i.i_state_fe, inflate(i.years_viable i.nb_setback_existence nb_wind_farm_existence county_gdp local_government_jobs extraction_gdp population_density republican_vote transmission_distance i.i_state_fe) robust iterate(100)
	
	capture return scalar seed = `first'
	capture return scalar b_s_years_viable_2 = el(e(b_mi),1,2)
	capture return scalar b_s_years_viable_3 = el(e(b_mi),1,3)
	capture return scalar b_s_years_viable_4 = el(e(b_mi),1,4)
	capture return scalar b_s_nb_stringency_2 = el(e(b_mi),1,6)
	capture return scalar b_s_nb_stringency_3 = el(e(b_mi),1,7)
	capture return scalar b_s_nb_stringency_4 = el(e(b_mi),1,8)
	capture return scalar b_s_nb_stringency_5 = el(e(b_mi),1,9)
	capture return scalar b_s_nb_wind_farm_count = el(e(b_mi),1,10)
	capture return scalar b_i_years_viable_2 = el(e(b_mi),1,35)
	capture return scalar b_i_years_viable_3 = el(e(b_mi),1,36)
	capture return scalar b_i_years_viable_4 = el(e(b_mi),1,37)
	capture return scalar b_i_nb_existence = el(e(b_mi),1,39)
	capture return scalar b_i_nb_wind_farm_existence = el(e(b_mi),1,40)
	
end

forval p_ban = 0(0.1)1 {
	

	bootstrap seeds = r(seed) b_s_years_viable_2 = r(b_s_years_viable_2) b_s_years_viable_3 = r(b_s_years_viable_3) b_s_years_viable_4 = r(b_s_years_viable_4) b_s_nb_wind_farm_count = r(b_s_nb_wind_farm_count) b_s_nb_stringency_2 = r(b_s_nb_stringency_2) b_s_nb_stringency_3 = r(b_s_nb_stringency_3) b_s_nb_stringency_4 = r(b_s_nb_stringency_4) b_s_nb_stringency_5 = r(b_s_nb_stringency_5) b_i_years_viable_2 = r(b_i_years_viable_2) b_i_years_viable_3 = r(b_i_years_viable_3) b_i_years_viable_4 = r(b_i_years_viable_4) b_i_nb_existence = r(b_i_nb_existence) b_i_nb_wind_farm_existence = r(b_i_nb_wind_farm_existence), reps(250) saving("Results/Sensitivity_Fig_D1/bootstrap_250_`p_ban'_3Aug2021.dta", every(1) double replace) : myboot `p_ban'

}
